//
//  ScrollViewViewController.m
//  享动
//
//  Created by 沈浩hh on 16/4/8.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "ScrollViewViewController.h"

@interface ScrollViewViewController ()<UIWebViewDelegate>

@property (nonatomic, strong)UIWebView *webView;

@end

@implementation ScrollViewViewController

- (void)loadData
{
    NSDictionary *parDic = @{@"article_id":_article_id};
    [RequestManager requestWithUrl:@"http://media.api.thejoyrun.com/article-get" requestType:RequestTypePost parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = dic[@"data"];
        
        dispatch_async(dispatch_get_main_queue(), ^{
          
            
            _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, KWIDTH, KHEIGHT - 49)];
//            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:dataDic[@"article_url"]]];
//            [_webView loadRequest:request];
            NSData *html = [NSData dataWithContentsOfURL:[NSURL URLWithString:dataDic[@"article_url"]]];
            
            NSString *string = [[NSString alloc]initWithData:html encoding:NSUTF8StringEncoding];
            NSLog(@"%@", string);
            NSRange range1 = [string rangeOfString:@"<div class=\"read_collect\">阅读&nbsp"];
            NSRange rang2 = [string rangeOfString:@"var ua = navigator.userAgent"];
            NSInteger a = range1.location;
            NSInteger b = rang2.location - range1.location - 33;
            NSRange rang3 = NSMakeRange(a, b);
            NSString *stringTemp1 = @"";
            NSString *stringTemp =[string stringByReplacingCharactersInRange:rang3 withString:stringTemp1];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文为悦跑圈跑友来稿，转载请注明出处及链接" withString:@"本文转自悦跑圈"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文为悦跑圈跑友稿件，转载请注明出处及链接" withString:@"本文转自悦跑圈"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文为悦跑圈跑友投稿，转载请注明出处及链接" withString:@"本文转自悦跑圈"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文来自悦跑圈跑友投稿，转载请注明出处及链接" withString:@"本文转自悦跑圈"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文为悦跑圈原创稿件，转载请注明出处及链接" withString:@"本文转自悦跑圈"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文为悦跑圈独家约稿，转载请注明出处及链接" withString:@"本文转自悦跑圈"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文为悦跑圈专栏作者约稿，转载请注明出处及链接" withString:@"本文转自悦跑圈"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"本文为悦跑圈专栏作者稿件，转载请注明出处及链接" withString:@"本文转自悦跑圈"];
            stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@"悦跑" withString:@"享动"];
            stringTemp = [NSString stringWithFormat:@"<head><style>img{width:%fpx !important;}</style></head>%@",KWIDTH - 15,stringTemp];
            [_webView loadHTMLString:stringTemp baseURL:[NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath]];

            _webView.delegate = self;
            _webView.backgroundColor = [UIColor clearColor];
            [self.view addSubview:_webView];

            
        });
        
    } error:^(NSError *error) {
        NSLog(@"错误");
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction:)];
    self.navigationItem.leftBarButtonItem = rightBarButton;
    [self loadData];
}

- (void)rightBarButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
